﻿using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Channels;
using MockingBird.Simulator.Interceptors.Wcf.Contracts;
using MockingBird.Simulator.Interceptors.Wcf.Contracts.Clients;

namespace MockingBird.Simulator.Interceptors.Wcf.SimulatorTester
{
    public class SimulatorTester
    {
        private static int loopCount = 100;
        private static int threadCount = 2 * loopCount;
        internal const string Action1 = "urn:mockingbird.simulator.interceptors.wcf.simulatorservice:action1";
        internal const string Action2 = "urn:mockingbird.simulator.interceptors.wcf.simulatorservice:action2";

        public void TestIOneWay()
        {
            Console.WriteLine("Starting test TestIOneWay...");

            using (IOneWayClient client = new IOneWayClient("IOneWay"))
            {
                Message request1 = Message.CreateMessage(MessageVersion.Soap11, Action1, new DummyMessage("Action1", "IOneWay"));
                Message request2 = Message.CreateMessage(MessageVersion.Soap11, Action2, new DummyMessage("Action2", "IOneWay"));

                client.ProcessMessage(request1);
                client.ProcessMessage(request2);
            }

            Console.WriteLine("Done.");
        }

        public void TestITwoWay()
        {
            Console.WriteLine("Starting test TestITwoWay...");

            ChannelFactory<ITwoWayChannel> cf = new ChannelFactory<ITwoWayChannel>("ITwoWay");
            ITwoWayChannel channel = cf.CreateChannel();

            Message request1 = Message.CreateMessage(MessageVersion.Soap11, Action1, new DummyMessage("Action1", "ITwoWay"));
            Message response1 = channel.ProcessMessage(request1);

            DummyMessage dm = response1.GetBody<DummyMessage>();
            Trace.Assert("Action1".Equals(dm.Action), "Incorrect DummyMessage.Action");

            Message request2 = Message.CreateMessage(MessageVersion.Soap11, Action2, new DummyMessage("Action2", "ITwoWay"));
            Message response2 = channel.ProcessMessage(request2);

            dm = response2.GetBody<DummyMessage>();
            Trace.Assert("Action2".Equals(dm.Action), "Incorrect DummyMessage.Action");

            Console.WriteLine("Done.");
        }

        public void TestITwoWay_netNamedPipe()
        {
            Console.WriteLine("Starting test TestITwoWay_netNamedPipe...");

            ChannelFactory<ITwoWayChannel> cf = new ChannelFactory<ITwoWayChannel>("ITwoWay_netNamedPipe");
            ITwoWayChannel channel = cf.CreateChannel();

            Message request1 = Message.CreateMessage(MessageVersion.Default, Action1, new DummyMessage("Action1", "ITwoWay_netNamedPipe"));
            Message response1 = channel.ProcessMessage(request1);

            DummyMessage dm = response1.GetBody<DummyMessage>();
            Trace.Assert("Action1".Equals(dm.Action), "Incorrect DummyMessage.Action");

            Message request2 = Message.CreateMessage(MessageVersion.Default, Action2, new DummyMessage("Action2", "ITwoWay_netNamedPipe"));
            Message response2 = channel.ProcessMessage(request2);

            dm = response2.GetBody<DummyMessage>();
            Trace.Assert("Action2".Equals(dm.Action), "Incorrect DummyMessage.Action");

            Console.WriteLine("Done.");
        }

        public void TestITwoWay_netTcp()
        {
            Console.WriteLine("Starting test TestITwoWay_netTcp...");

            ChannelFactory<ITwoWayChannel> cf = new ChannelFactory<ITwoWayChannel>("ITwoWay_netTcp");
            ITwoWayChannel channel = cf.CreateChannel();

            Message request1 = Message.CreateMessage(MessageVersion.Default, Action1, new DummyMessage("Action1", "ITwoWay_netTcp"));
            Message response1 = channel.ProcessMessage(request1);

            DummyMessage dm = response1.GetBody<DummyMessage>();
            Trace.Assert("Action1".Equals(dm.Action), "Incorrect DummyMessage.Action");

            Message request2 = Message.CreateMessage(MessageVersion.Default, Action2, new DummyMessage("Action2", "ITwoWay_netTcp"));
            Message response2 = channel.ProcessMessage(request2);

            dm = response2.GetBody<DummyMessage>();
            Trace.Assert("Action2".Equals(dm.Action), "Incorrect DummyMessage.Action");

            Console.WriteLine("Done.");
        }

        public void TestITwoWayVoid()
        {
            Console.WriteLine("Starting test TestIOneWay...");

            ChannelFactory<ITwoWayVoidChannel> cf = new ChannelFactory<ITwoWayVoidChannel>("ITwoWayVoid");
            ITwoWayVoidChannel channel = cf.CreateChannel();

            Message request1 = Message.CreateMessage(MessageVersion.Soap11, Action1, new DummyMessage("Action1", "ITwoWayVoid"));
            Message request2 = Message.CreateMessage(MessageVersion.Soap11, Action2, new DummyMessage("Action2", "ITwoWayVoid"));

            channel.ProcessMessage(request1);
            channel.ProcessMessage(request2);

            Console.WriteLine("Done.");
        }
    }
}
